package com.qqbot.utils;

import com.qqbot.enums.DatePattern;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 日期区间工具类
 *
 * @author 付亚全
 */
public class DateRangeUtil {
    /**
     * 获取范围内的时间列表
     *
     * @param startDate 起始时间
     * @param endDate   结束时间
     * @param dateField 变动时间域
     * @return 时间列表
     */
    public static List<Date> getBetweenDates(Date startDate, Date endDate, DateField dateField) {
        return getBetweenDates(startDate, endDate, dateField, true, true, 1);
    }

    /**
     * 按条件计算范围内时间列表
     *
     * @param startDate    起始时间
     * @param endDate      结束时间
     * @param dateField    变化域，默认：日
     * @param includeStart 是否包含第一个结果
     * @param includeEnd   是否包含最后一个结果
     * @param step         变化步长，默认：1
     * @return 时间集合
     */
    public static List<Date> getBetweenDates(Date startDate, Date endDate, DateField dateField, Boolean includeStart,
                                             Boolean includeEnd, Integer step) {
        if (null == startDate || null == endDate) {
            return null;
        }
        DatePattern pattern = matchPatternByField(dateField);
        startDate = DateFormatUtil.reformat(DateFormatUtil.format(startDate, pattern), pattern);
        endDate = DateFormatUtil.reformat(DateFormatUtil.format(endDate, pattern), pattern);
        List<Date> resultList = new ArrayList<>();
        if (startDate.before(endDate)) {
            while (!startDate.after(endDate)) {
                resultList.add(startDate);
                startDate = DateUtil.addField(startDate, dateField, step);
            }
        } else {
            while (!startDate.before(endDate)) {
                resultList.add(startDate);
                startDate = DateUtil.addField(startDate, dateField, -1 * step);
            }
        }
        if (!includeStart) resultList.remove(0);
        if (!includeEnd) resultList.remove(resultList.size() - 1);
        return resultList;
    }

    private static DatePattern matchPatternByField(DateField dateField) {
        DatePattern pattern = DatePattern.YYYY_MM_DD;
        switch (dateField) {
            case YEAR:
                pattern = DatePattern.YYYY;
                break;
            case MONTH:
                pattern = DatePattern.YYYY_MM;
                break;
            case DATE:
                pattern = DatePattern.YYYY_MM_DD;
                break;
            case HOUR:
                pattern = DatePattern.YYYY_MM_DD_HH;
                break;
            case WEEK:
                pattern = DatePattern.YYYY_WW;
                break;
            case MINUTE:
                pattern = DatePattern.YYYY_MM_DD_HH_MM;
                break;
            case SECOND:
                pattern = DatePattern.YYYY_MM_DD_HH_MM_SS;
                break;
            case MILLISECOND:
                pattern = DatePattern.YYYY_MM_DD_HH_MM_SS_SSS;
                break;
            case SEASON:
                pattern = DatePattern.YYYY_EE;
                break;
            case HALF_YEAR:
                pattern = DatePattern.YYYY_FF;
                break;
        }
        return pattern;
    }
}
